
#include "stdafx.h"
#include "customsite.h"
#include "webcontrol.h"
#include "webcontainer.h"
#include <strsafe.h>

CCustomSite::CCustomSite(CWebContainer* pWebContainer)
: m_pWebContainer(pWebContainer)
{
	ATLASSERT(pWebContainer);
}

CCustomSite::~CCustomSite()
{
}

// IUnknown
STDMETHODIMP CCustomSite::QueryInterface(const IID &riid, void **ppvObject)
{
	if (ppvObject == NULL)
		return E_INVALIDARG;

	*ppvObject = NULL ;

	if (riid == IID_IUnknown || riid == IID_IOleClientSite)
		*ppvObject = (IOleClientSite*)this; 
	else if (riid == IID_IOleCommandTarget) 
		*ppvObject = (IOleCommandTarget*)this; 
	else if (riid == IID_IDocHostUIHandler || riid == IID_IDocHostUIHandler2) 
		*ppvObject = (IDocHostUIHandler2*)this; 
	else if (riid == IID_IDocHostShowUI)
		*ppvObject = (IDocHostShowUI*)this;
	else if (riid == IID_IServiceProvider)
		*ppvObject = (IServiceProvider*)this ;
	else if (riid == IID_IOleInPlaceSite)
		*ppvObject = (IOleInPlaceSite*)this; 

	if (*ppvObject != NULL)
	{
		((LPUNKNOWN)*ppvObject)->AddRef();
		return S_OK;
	}

	return E_NOINTERFACE ;
}

STDMETHODIMP_(ULONG) CCustomSite::AddRef()
{
	return 1;
}

STDMETHODIMP_(ULONG) CCustomSite::Release()
{
	return 1;
}

// IOleClientSite
STDMETHODIMP CCustomSite::SaveObject(void)
{
	ATLASSERT(0);
	return E_NOTIMPL ;
}

STDMETHODIMP CCustomSite::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk)
{
	ATLASSERT(0);
	return E_NOTIMPL ;
}

STDMETHODIMP CCustomSite::GetContainer(IOleContainer **ppContainer)
{
	*ppContainer = NULL;
	return E_NOINTERFACE;
}

STDMETHODIMP CCustomSite::ShowObject(void)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnShowWindow(BOOL fShow)
{
	ATLASSERT(0);
	return E_NOTIMPL ;
}

STDMETHODIMP CCustomSite::RequestNewObjectLayout(void)
{
	ATLASSERT(0);
	return E_NOTIMPL ;
}

// IOleCommandTarget
STDMETHODIMP CCustomSite::QueryStatus(const GUID *pguidCmdGroup, ULONG cCmds, OLECMD* prgCmds, OLECMDTEXT *pCmdText)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CCustomSite::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
{
#ifndef _DEBUG
	HRESULT hr = OLECMDERR_E_UNKNOWNGROUP ;
	if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler))
	{
		switch (nCmdID) 
		{
		case OLECMDID_SHOWSCRIPTERROR:
			{
				pvaOut->vt = VT_BOOL ;
				pvaOut->boolVal = VARIANT_TRUE ;

				return S_OK ;
			}
			break ;

		default:
			hr = OLECMDERR_E_NOTSUPPORTED;
			break;
		}
	}

	return hr;

#else

	return OLECMDERR_E_NOTSUPPORTED;

#endif
}

//IDocHostUIHandler
STDMETHODIMP CCustomSite::ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
{
#ifndef _DEBUG
	return S_OK;
#else
	return E_FAIL;
#endif
}

STDMETHODIMP CCustomSite::GetHostInfo(DOCHOSTUIINFO *pInfo)
{
	pInfo->cbSize = sizeof(DOCHOSTUIINFO) ;
	//pInfo->dwFlags |= DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_THEME | DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE;
	pInfo->dwFlags |= DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_THEME;
	pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT ;

	return S_OK ;
}

STDMETHODIMP CCustomSite::ShowUI(DWORD dwID, IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget, IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
{
	return S_OK ;
}

STDMETHODIMP CCustomSite::HideUI(void)
{
	return S_OK ;
}

STDMETHODIMP CCustomSite::UpdateUI(void)
{
	return S_OK ;
}

STDMETHODIMP CCustomSite::EnableModeless(BOOL fEnable)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnDocWindowActivate(BOOL fActivate) 
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnFrameWindowActivate(BOOL fActivate)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::ResizeBorder(LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::TranslateAccelerator(LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
{
	return S_FALSE;
}

STDMETHODIMP CCustomSite::GetOptionKeyPath(LPOLESTR *pchKey, DWORD dw)
{
	HRESULT hr = E_INVALIDARG;

	if (pchKey != NULL)
	{
		size_t cchLength;
		LPWSTR szMyKey = L"Software\\MyBook\\Explorer";
		hr = StringCchLengthW(szMyKey, MAX_PATH, &cchLength);

		*pchKey = (LPOLESTR)AtlCoTaskMemCAlloc(cchLength + 1, sizeof(WCHAR));
		if (*pchKey)
			hr = StringCchCopyW(*pchKey, cchLength + 1, szMyKey);

		hr = (*pchKey) ? S_OK : E_OUTOFMEMORY;
	}

	return hr;
}

STDMETHODIMP CCustomSite::GetDropTarget(IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
{
	//return S_FALSE;
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::GetExternal(IDispatch **ppDispatch)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CCustomSite::TranslateUrl(DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
{
	*ppchURLOut = NULL;
	return S_FALSE;
}

STDMETHODIMP CCustomSite::FilterDataObject(IDataObject *pDO, IDataObject **ppDORet) 
{
	*ppDORet = NULL;
	return S_FALSE;
}

// IDocHostUIHandler2
STDMETHODIMP CCustomSite::GetOverrideKeyPath(LPOLESTR *pchKey, DWORD dw)
{
	return E_NOTIMPL;
}

// IDocHostShowUI
STDMETHODIMP CCustomSite::ShowMessage(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult)
{
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::ShowHelp(HWND hwnd, LPOLESTR pszHelpFile, UINT uCommand, DWORD dwData, POINT ptMouse, IDispatch *pDispatchObjectHit)
{
	return E_NOTIMPL;
}

// IServiceProvider
STDMETHODIMP CCustomSite::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
{
	*ppvObject = NULL;
	return E_NOINTERFACE;
}

// IOleWindow
STDMETHODIMP CCustomSite::GetWindow(HWND *phwnd)
{
	*phwnd = m_pWebContainer->m_hWnd;
	return S_OK;
}

STDMETHODIMP CCustomSite::ContextSensitiveHelp(BOOL fEnterMode)
{ 
	ATLASSERT(0);
	return E_NOTIMPL;
}

// IOleInPlaceSite : IOleWindow
STDMETHODIMP CCustomSite::CanInPlaceActivate()
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnInPlaceActivate()
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnUIActivate()
{
	return S_OK;
}

STDMETHODIMP CCustomSite::GetWindowContext(IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
	*ppDoc = 0;

	lpFrameInfo->fMDIApp = FALSE;
	lpFrameInfo->hwndFrame = m_pWebContainer->m_hWnd;
	lpFrameInfo->haccel = 0;
	lpFrameInfo->cAccelEntries = 0;

	return S_OK;
}

STDMETHODIMP CCustomSite::Scroll(SIZE scrollExtant)
{ 
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::OnUIDeactivate(BOOL fUndoable)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::OnInPlaceDeactivate()
{
	return S_OK;
}

STDMETHODIMP CCustomSite::DiscardUndoState()
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::DeactivateAndUndo()
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::OnPosRectChange(LPCRECT lprcPosRect)
{
	CComPtr<IOleInPlaceObject> spOleInPlaceObject;
	CComPtr<IOleObject> spOleObject = m_pWebContainer->m_pWebControl->GetOleObject();

	if (spOleObject && SUCCEEDED(spOleObject->QueryInterface(IID_IOleInPlaceObject, (void**)&spOleInPlaceObject)))
		spOleInPlaceObject->SetObjectRects(lprcPosRect, lprcPosRect);

	return S_OK;
}

// IOleInPlaceUIWindow
STDMETHODIMP CCustomSite::GetBorder(LPRECT lprectBorder)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::RequestBorderSpace(LPCBORDERWIDTHS pborderwidths)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::SetBorderSpace(LPCBORDERWIDTHS pborderwidths)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::SetActiveObject(IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
{
	return S_OK;
}

// IOleInPlaceFrame
STDMETHODIMP CCustomSite::InsertMenus(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::SetMenu(HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::RemoveMenus(HMENU hmenuShared)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}

STDMETHODIMP CCustomSite::SetStatusText(LPCOLESTR pszStatusText)
{
	return S_OK;
}

STDMETHODIMP CCustomSite::TranslateAccelerator(LPMSG lpmsg, WORD wID)
{
	ATLASSERT(0);
	return E_NOTIMPL;
}
